package com.yoomiito.app.ui.order.sureorder.shopcar;

import android.text.TextUtils;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.base.BaseLazyFragmentPresenter;
import com.yoomiito.app.model.order.ShopCarList;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.utils.L;
import com.yoomiito.app.utils.YMToast;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;

public class ShopCarPresenter extends BaseLazyFragmentPresenter<ShopCarFragment> {
    public ShopCarPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    public void getList() {
        YmApi.getYmService()
                .showCarList(1, 1000)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<ShopCarList>(handleException()) {
                    @Override
                    public void onSuccess(ShopCarList shopCarList) {
                        if (hasV()) {
                            getV().dismissLoading();
                            getV().setData(shopCarList);
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV())
                            getV().dismissLoading();
                    }
                });
    }

    public void addShopCar(String productId, int num, int position) {
        YmApi.getYmService()
                .addShopCar(productId, num)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        if (hasV()){
                            getV().dismissLoading();
                            getV().numChangeSuccess(position, num);
                            YMToast.showText("修改成功");
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV()){
                            getV().dismissLoading();
                            getV().numChangeFail();
                            if (!TextUtils.isEmpty(error.getMsg()))
                                YMToast.showText(error.getMsg());
                        }
                    }
                });

    }

    public void delGoodsFromShopCar(String productId) {
        L.i("删除购物车参数：" + productId);
        YmApi.getYmService()
                .delGoodsFromShopCar(1)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        if (hasV()){
                            getV().dismissLoading();
                            YMToast.showText("删除成功");
                            getV().delSuccess();
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {
                        if (hasV()){
                            getV().dismissLoading();
                            if (!TextUtils.isEmpty(error.getMsg()))
                                YMToast.showText(error.getMsg());
                        }
                    }
                });
    }
}
